package com.image;

import cn.hutool.core.img.Img;
import cn.hutool.core.io.FileUtil;
import com.File.FileTest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.awt.*;
import java.io.File;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author 航
 * @date 2024/07/09 13:49
 **/
public class HuToolImageCut {

    private static final Logger logger = LogManager.getLogger(HuToolImageCut.class);


    public static void main(String[] args) {
        HuToolImageCut huToolImageCut = new HuToolImageCut();
//        huToolImageCut.cutImage("C:\\Users\\34572\\Desktop\\新建文件夹\\100.jpg",
//                "C:\\Users\\34572\\Desktop\\新建文件夹\\200.jpg");

        // 线程池
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        String path = "E:\\MyPhoneData\\data1\\D16发育快";
        Set<String> set = Stream.of("jpg","png", "bmp", "JPG", "PNG").collect(Collectors.toSet());
        FileTest fileTest = new FileTest();
        List<File> files = FileUtil.loopFiles(path, (f) -> fileTest.checkSuffix(f, set));

        for (File file : files){
            executorService.submit(() -> {
                try {
                    logger.info("{}-->{}",Thread.currentThread().getName(), file);
                    huToolImageCut.cutImage(file.getPath(), file.getParent() + "\\" + "new_" + file.getName());
                    if(!file.delete()){
                        logger.info("Deleted：{}",file.getPath());
                    }
                }catch (Exception e){
                    logger.error(e.getMessage());
                }
            });
        }
       executorService.shutdown();
    }

    /**
     * 剪切掉图片的第一行像素
     * @param oldPath
     * @param newPath
     */
    public void cutImage(String oldPath, String newPath){
        try{
            // 创建Img对象，加载图片
            Img img = Img.from(new File(oldPath));
            // 定义裁剪区域的Rectangle对象
            Rectangle rectangle = new Rectangle(0, 1,
                    img.getImg().getWidth(null),
                    img.getImg().getHeight(null) - 1);
            // 裁剪图片
            img.cut(rectangle);
            img.write(new File(newPath));
        }catch (Exception e){
            logger.error(e.getMessage());
        }
    }
}
